package com.dyw.ycgovfinance.http;

import com.dyw.ycgovfinance.util.LogUtil;
import com.dyw.ycgovfinance.util.MD5Util;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2016/8/26.
 */
public class HttpServiceUtil {






    //请求相应的key
    public static final String RESOIBSE_KEY_CODE = "code";
    public static final String RESOIBSE_KEY_MESSAGE = "msg";
    // d对应列表数据
    public static final String RESOIBSE_KEY_LIST = "list";

    public static final String RESOIBSE_KEY_STATUS = "status";


    //请求相应的状态位
    public static final String RESOIBSE_STATE_SUCCESS = "200";
    public static final String RESOIBSE_STATE_FAIL = "300";
    public static final String RESOIBSE_STATE_TOKEN_WRONG = "402";

    //构建请求参数
    //这样构建可以传gson数据到服务器
    public static RequestBody makeRequestBody(Map<String,String> paramsMap)
    {
        Gson gson=new Gson();
        String paramsJsonStr = gson.toJson(paramsMap);
        LogUtil.e("paramJsonStr:"+paramsJsonStr);
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(JSON,paramsJsonStr);
        return requestBody;
    }


    public static RequestBody makeRequestBodyByFormData(Map<String,String> paramsMap)
    {
        Gson gson=new Gson();
        String paramsJsonStr = gson.toJson(paramsMap);
        LogUtil.e("paramJsonStr:"+paramsJsonStr);
        MediaType mdTypeFormData = MediaType.parse("multipart/form-data");
        RequestBody requestBody = RequestBody.create(mdTypeFormData,paramsJsonStr);
        return requestBody;
    }

    public static MultipartBody.Part makeFileRequestBody(File file)
    {
        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);

        MultipartBody.Part body =
                MultipartBody.Part.createFormData("image", "resumepic_" + file.getName(), requestFile);



        /*
        RequestBody picTypeRequestBody =
                RequestBody.create(MediaType.parse("multipart/form-data"), picType);*/

        return body;
    }




    //根据参数列表和值列表 获取参数值列表
    public static List<String> getParamValueList(List<String> paramList, List<String> valueList)
    {

        List<String> paramValueList = new ArrayList<>();

        for (int i = 0;i < valueList.size();i++)
        {
            if (null!=valueList.get(i))
            {
                StringBuilder paramValueStrBuilder = new StringBuilder();
                paramValueStrBuilder.append(paramList.get(i))
                                    .append("=")
                                    .append(valueList.get(i));
                paramValueList.add(paramValueStrBuilder.toString());
            }
        }

        return paramValueList;
    }

    //根据请求参数转成json字符串
    public static String getJsonValueByRequestParams(HashMap<String,String> requestParams)
    {
        return new Gson().toJson(requestParams);
    }

    //根据pkey和timeZone的值获取sign字段
    public static String getSignValueByPKeyAndTimeZone(String pKey, String timeZone)
    {
        String originalStr = new StringBuilder()
                                    .append(pKey)
                                    .append("\\\\\\\\\\")
                                    .append(timeZone)
                                    .toString();
        LogUtil.e("originalStr",originalStr);

        String MD5Str = MD5Util.MD5(originalStr);
        LogUtil.e("MD5Str",MD5Str);

        return MD5Str;
    }

    //获取sign值
   /* 1.把参数和值构建出参数值list（去掉值为空的参数值）
    2.对参数值列表进行正向排序
    3.对已经正向排序的参数值列表倒序
    4.循环参数值列表,拼接处参数值字符串
    5.md5加密参数值字符串*/
    public static String getSignValue(List<String> paramList, List<String> valueList)
    {

        List<String> paramValueList = getParamValueList(paramList,valueList);


        Collections.sort(paramValueList);
        Collections.reverse(paramValueList);

        StringBuilder signStrBuilder = new StringBuilder();
        signStrBuilder.append("4002306828fb8779259b06da59b0551e");
        for (int i = 0 ;i< paramValueList.size();i++)
        {
            if (i==0)
            {
                signStrBuilder.append(paramValueList.get(i));
            }else {
                signStrBuilder.append("&");
                signStrBuilder.append(paramValueList.get(i));
            }
        }
        signStrBuilder.append("4002306828fb8779259b06da59b0551e");
        LogUtil.e("paramsJson:"+signStrBuilder.toString());
        return MD5Util.MD5(signStrBuilder.toString()).toLowerCase();

    }


    public static void main(String args[])
    {
        List<String> paramList = new ArrayList<>();
        paramList.add("deviceType");
        paramList.add("time");
        paramList.add("numPerPage");
        paramList.add("currentPage");
        paramList.add("workJob");
        paramList.add("key");
        paramList.add("district");
        paramList.add("sdistrict");
        paramList.add("settr");
        paramList.add("wage");
        paramList.add("nature");
        paramList.add("scale");


        List<String> valueList = new ArrayList<>();
        valueList.add("android");
        valueList.add("2016-08-26 17:06:30");
        valueList.add(null);
        valueList.add(null);
        valueList.add("岗位");
        valueList.add("关键字");
        valueList.add("");
        valueList.add("子区域");
        valueList.add("发布时间");
        valueList.add("工资水平");
        valueList.add("性质");
        valueList.add("规模");

        List<String> paramValueList = getParamValueList(paramList,valueList);

        System.out.println(getSignValue(paramList,valueList));
        //System.out.println(getSignValue(paramValueList));
    }
}
